package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Organization;
import cn.wolfcode.crm.query.OrganizationObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IOrganizationService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("organization")
public class OrganizationController {
    @Autowired
    private IOrganizationService organizationMapper;

    @RequiresPermissions("organization:index")
    @PermissionName("机构信息主页")
    @RequestMapping("index")
    public String index( @ModelAttribute("qo") OrganizationObjectQuery qo) {
        return "organization/list";
    }

    @RequiresPermissions("organization:list")
    @PermissionName("机构信息查询")
    @RequestMapping("list")
    @ResponseBody
    public PageResult<Organization> list( @ModelAttribute("qo") OrganizationObjectQuery qo) {
        PageResult<Organization> list = organizationMapper.query(qo);
        return list;
    }
    @RequestMapping("query")
    @ResponseBody
    public List<Organization> query(@ModelAttribute("qo") OrganizationObjectQuery qo) {
        return organizationMapper.getAll();
    }


    @RequiresPermissions("organization:delete")
    @PermissionName("机构信息删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                organizationMapper.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除机构信息失败");
        }
        return jsonResult;
    }

    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                organizationMapper.changState(id);
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除失败");
//            throw new RuntimeException("机构信息信息删除失败");
        }
        return jsonResult;
    }


    @RequiresPermissions("organization:saveOrUpdate")
    @PermissionName("机构信息创建更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Organization organization) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (organization.getId() != null) {
                organizationMapper.update(organization);
            } else {
                organizationMapper.save(organization);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("保存或更新机构信息失败");
        }
        return jsonResult;
    }


    @RequiresPermissions("organization:queryOrg")
    @PermissionName("机构信息查询")
    @RequestMapping("queryOrg")
    @ResponseBody
    public List<Organization> queryOrg() {
        List<Organization> list = organizationMapper.queryOrg();
        return list;
    }


}
